package com.gsta.ifishery.common.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期工具类 . <br>
 * 
 * @author hkb <br>
 */
public final class DateUtils {

    /**
     * 线程安全的日期格式对象
     */
    private static final ThreadLocal<DateFormat> DATE_FORMAT = new ThreadLocal<DateFormat>() {

        @Override
        protected DateFormat initialValue() {
            // 完整日期
            return new SimpleDateFormat("yyMMddHHmmss");
        }

    };

    /**
     * 私有构造函数
     */
    private DateUtils() {
        super();
    }

    /**
     * 格式化日期
     * 
     * @param date
     * @return yyMMddHHmmss格式的字符串
     */
    public static String formatDate(Date date) {
        return DATE_FORMAT.get().format(date);
    }

    /**
     * 获取响应时间
     * 
     * @return yyMMddHHmmss格式的字符串
     */
    public static String getResponseTime() {
        Calendar calendar = Calendar.getInstance();
        return formatDate(calendar.getTime());
    }

    /**
     * 获取相差多少分钟
     * 
     * @param begin
     * @param end
     * @return
     */
    public static long getMinuteDifference(long begin, long end) {
        long between = end - begin;
        long day = between / (24 * 60 * 60 * 1000);
        long hour = (between / (60 * 60 * 1000) - day * 24);
        return ((between / (60 * 1000)) - day * 24 * 60 - hour * 60);
    }

}
